

*******************************
* FIRST STAGE PLOT AND F-TEST *   
*******************************

use "${build}/iv_fin_lehd_w_comp_weights.dta", clear
replace black = 0 if black_miss == 1
replace fem = 0 if fem_miss == 1

global stub = "all"

**************************
*Regression specification*
**************************
global cluster_ols="judgexyear "
global cluster_iv="judgexyear "
global stem = ""
global instrument="loo_vc_ivsamp100"
global fe="districtxyearxquarter"

replace noprior = 0 if noprior_miss == 1
cap gen joint_miss = joint == . 
cap gen age_miss = age == . 

foreach out in "anyearn_allst" "earn"   {
	foreach bval in "b1" "b2" "b3" "b4" "b5" "b6" "b7" "b8" "b9" "b10" "b11" "b12" "b13" "b14" "b15" "b16" "b4to1" "b8to1"  "b12to9" "b8to5" {
	        cap gen `out'_`bval'_miss = `out'_`bval' == .
		replace `out'_`bval'= 0 if `out'_`bval'_miss == 1
	}
}

macro define controls="noprior noprior_miss joint ad_damnum ageatcase ageatcase_sq ageatcase_cub fem black white fem_black fem_white hispanic fem_hisp  age_miss fem_miss black_miss hispanic_miss nbrhd_pov_rate_5yr nbrhd_med_rent_5yr nbrhd_pov_rate_5yr_miss nbrhd_med_rent_5yr_miss "

global earnlags = "earn_b1 earn_b2 earn_b3 earn_b4 earn_b1_miss earn_b2_miss earn_b3_miss earn_b4_miss earn_b8to1 earn_b8to1_miss"
global emplags = "anyearn_allst_b1 anyearn_allst_b2 anyearn_allst_b3 anyearn_allst_b4 anyearn_allst_b1_miss anyearn_allst_b2_miss anyearn_allst_b3_miss anyearn_allst_b4_miss anyearn_allst_b8to1 anyearn_allst_b8to1_miss"

global earnlags_b8to5 = " earn_b5 earn_b6 earn_b7 earn_b8 earn_b5_miss earn_b6_miss earn_b7_miss earn_b8_miss "
global earnlags_b12to9 = " earn_b9 earn_b10 earn_b11 earn_b12 earn_b9_miss earn_b10_miss earn_b11_miss earn_b12_miss "
global earnlags_b16to13 = " earn_b13 earn_b14 earn_b15 earn_b16 earn_b13_miss earn_b14_miss earn_b15_miss earn_b16_miss "

global emplags_b8to5 = " anyearn_allst_b5 anyearn_allst_b6 anyearn_allst_b7 anyearn_allst_b8 anyearn_allst_b5_miss     anyearn_allst_b6_miss anyearn_allst_b7_miss anyearn_allst_b8_miss "
global emplags_b12to9 = " anyearn_allst_b9 anyearn_allst_b10 anyearn_allst_b11 anyearn_allst_b12 anyearn_allst_b9_miss anyearn_allst_b10_miss anyearn_allst_b11_miss anyearn_allst_b12_miss "
global emplags_b16to13 = " anyearn_allst_b13 anyearn_allst_b14 anyearn_allst_b15 anyearn_allst_b16 anyearn_allst_b13_miss anyearn_allst_b14_miss anyearn_allst_b15_miss anyearn_allst_b16_miss "

macro define all_controls=" $controls $earnlags $emplags"

**************************************
* FIRST STAGE ESTIMATES AND F-STATS  *
**************************************
*First stage with one instrument
gen trim_amnt_loo_vc_ivsamp100_s = trimamountloo_vc_ivsamp100/ 1000
global amnt_instrument = "trim_amnt_loo_vc_ivsamp100_s"

* FULL SAMPLE 
gen ones = 1

eststo first_stage1:  reg c_evict $instrument  i.$fe, r cluster($cluster_iv)
estadd local controls "FEs", replace
local obs1: di %12.2g e(N)
estadd local obs "`obs1'", replace
noisily disp `obs1'
unique pik if e(sample) & c_evict == 0 
local count_0 = r(sum)
estadd local count0 "`count_0'", replace
unique pik if e(sample) & c_evict == 1
local count_1 = r(sum)
estadd local count1 "`count_1'", replace
local f_all: di %12.2g  e(F)
estadd local F_all "`f_all'", replace
test $instrument 
local f_inst: di %12.2g r(F)
estadd local F_inst "`f_inst'", replace

* First stage regression with FEs and controls 
eststo first_stage2:  reg c_evict $instrument $controls i.$fe, r cluster($cluster_iv) 
estadd local controls "FEs+covs", replace
local obs1: di %12.2g e(N)
estadd local obs "`obs1'", replace
noisily disp `obs1'
unique pik if e(sample) & c_evict == 0 
local count_0 = r(sum)
estadd local count0 "`count_0'", replace
unique pik if e(sample) & c_evict == 1
local count_1 = r(sum)
estadd local count1 "`count_1'", replace
local f_all: di %12.2g  e(F)
estadd local F_all "`f_all'", replace
test $instrument 
local f_inst: di %12.2g r(F)
estadd local F_inst "`f_inst'", replace

*First stage with two instruments FEs and controls
eststo first_stage3:  reg c_evict $instrument $amnt_instrument $controls i.$fe if joint == 1, r cluster($cluster_iv) 
estadd local controls "FEs+covs", replace
local obs1: di %12.2g e(N)
estadd local obs "`obs1'", replace
noisily disp `obs1'
unique pik if e(sample) & c_evict == 0 
local count_0 = r(sum)
estadd local count0 "`count_0'", replace
unique pik if e(sample) & c_evict == 1
local count_1 = r(sum)
estadd local count1 "`count_1'", replace
local f_all: di %12.2g  e(F)
estadd local F_all "`f_all'", replace
test $instrument 
local f_inst: di %12.2g r(F)
estadd local F_inst "`f_inst'", replace
test $amnt_instrument 
local f_amnt: di %12.2g r(F)
estadd local F_amnt "`f_amnt'", replace
test $instrument $amnt_instrument 
local f_both: di %12.2g r(F)
estadd local F_both "`f_both'", replace

*First stage with two instruments FEs and controls
eststo first_stage4:  reg judgment_amount  $amnt_instrument $controls i.$fe if  c_evict == 1 & joint == 1 & $instrument!=., r cluster($cluster_iv) 
estadd local controls "FEs+covs", replace
local obs1: di %12.2g e(N)
estadd local obs "`obs1'", replace
noisily disp `obs1'
unique pik if e(sample) & c_evict == 0 
local count_0 = r(sum)
estadd local count0 "`count_0'", replace
unique pik if e(sample) & c_evict == 1
local count_1 = r(sum)
estadd local count1 "`count_1'", replace
local f_all: di %12.2g  e(F)
estadd local F_all "`f_all'", replace
test $amnt_instrument 
local f_amnt: di %12.2g r(F)
estadd local F_amnt "`f_amnt'", replace
	
esttab first_stage1 first_stage2 first_stage3 first_stage4 using "$build/lehd_first_stage_and_fstat_full.csv", ///
		 replace compress nomtitles  nodepvars wrap  nonotes label  se(3) b(3) ///
		eqlabels(none) keep( $instrument $amnt_instrument) star(* 0.10 ** 0.05 *** 0.01) ///
		stats(controls obs count1 count0 states1 states0 F_inst F_amnt F_both, fmt(0 3) ///
		 labels(`"Controls"'  ///
		 `"Number of observations"') ) 

***********************
* SUBSAMPLE ESTIMATES *
***********************
eststo clear 

foreach het in   "fem" "black"  { 
	eststo clear
	foreach samp of numlist 1 0 {
		di "ON het: `het', samp: `samp'"

		eststo first_stage1_`samp':  reg c_evict $instrument  i.$fe  if `het' == `samp', r cluster($cluster_iv) 
		estadd local group "`het'`samp'", replace
		estadd local controls "FEs", replace
		local obs1: di %12.2g e(N)
		estadd local obs "`obs1'", replace
		noisily disp `obs1'
		unique pik if e(sample) & c_evict == 0 
		local count_0 = r(sum)
		estadd local count0 "`count_0'", replace
		unique pik if e(sample) & c_evict == 1
		local count_1 = r(sum)
		estadd local count1 "`count_1'", replace
		local f_all: di %12.2g  e(F)
		estadd local F_all "`f_all'", replace
		test $instrument 
		local f_inst: di %12.2g r(F)
		estadd local F_inst "`f_inst'", replace

		* First stage regression with FEs and controls 
		eststo first_stage2_`samp':  reg c_evict $instrument $controls i.$fe  if `het' == `samp', r cluster($cluster_iv) 
		estadd local group "`het'`samp'", replace
		estadd local controls "FEs+covs", replace
		local obs1: di %12.2g e(N)
		estadd local obs "`obs1'", replace
		noisily disp `obs1'
		unique pik if e(sample) & c_evict == 0 
		local count_0 = r(sum)
		estadd local count0 "`count_0'", replace
		unique pik if e(sample) & c_evict == 1
		local count_1 = r(sum)
		estadd local count1 "`count_1'", replace
		local f_all: di %12.2g  e(F)
		estadd local F_all "`f_all'", replace
		test $instrument 
		local f_inst: di %12.2g r(F)
		estadd local F_inst "`f_inst'", replace
			
	}

	esttab first_stage1_0 first_stage2_0  first_stage1_1 first_stage2_1 using "$build/lehd_first_stage_and_fstat_`het'.csv", ///
			 replace compress nomtitles  nodepvars wrap  nonotes label  se(3) b(3) ///
			eqlabels(none) keep( $instrument) star(* 0.10 ** 0.05 *** 0.01) ///
			stats(group controls obs count1 count0 states1 states0 F_inst, fmt(0 3) ///
			 labels(`"Controls"'  ///
			 `"Number of observations"') ) 
	
}


import delimited "$build/lehd_first_stage_and_fstat_full.csv", clear 
tempfile fs
save `fs'

import delimited "$build/lehd_first_stage_and_fstat_fem.csv", clear 
tempfile fs2
save `fs2'

import delimited "$build/lehd_first_stage_and_fstat_black.csv", clear 
append using `fs'
append using `fs2'

gen type = "Mean" if !mi(A)
replace type = "SE" if mi(A)
replace A = A[_n-1] if mi(A)

drop if regexm(A, "Title|coefficients;|05")
drop if mi(B) & mi(C) & mi(D)

rename (A B C D) (variable evicted not_evicted acs)
reshape wide evicted not_evicted acs, i(variable) j(type) string
rename (*Mean *SE) (Mean_* SE_*)

foreach i of varlist SE* {
	replace `i' = subinstr(`i', "(", "", .)
	replace `i' = subinstr(`i', ")", "", .)
}

replace variable = "Obs" if variable=="Number of Observations"
gen sorting = 1 if variable=="Obs"
replace sorting = 0 if mi(sorting)
sort sorting variable
drop sorting
destring Mean* SE*, replace 
order variable Mean_evicted SE_evict Mean_not_evicted SE_not Mean_acs SE_acs
compress

export delimited using "${Inputs}\Cook_lehd_iv_fs.csv", replace

